//
//  Session.m
//  yiqizhangda
//
//  Created by bigknife on 14-2-2.
//  Copyright (c) 2014年 idongler. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Session.h"
#import "NSUserDefaults+RMSaveCustomObject.h"

@interface Session()
@property (nonatomic,strong)VMCUserDomain *currentUser;
@end
@implementation Session{
}


+ (instancetype)sharedInstance{
    static dispatch_once_t once;
    static Session * sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
		DLog("Session第一次创建 : %@", self);
    });
    return sharedInstance;
}
#define SESSION_USER_KEY	@"currentUser"
#define SESSION_CITY_KEY	@"currentCity"
#define SESSION_TOKEN_KEY	@"token"
#define SESSION_REMIND_KEY	@"remind"
#define SESSION_ASKTYPE_KEY @"askType"

- (void)saveSession:(NSString *)token{
	[[NSUserDefaults standardUserDefaults] setObject:token forKey:SESSION_TOKEN_KEY];
}

- (void)loginWithUser:(VMCUserDomain *)user andToken:(NSString *)token {
    self.currentUser = user;
    //写入存储
    [[NSUserDefaults standardUserDefaults] rm_setCustomObject:user forKey:SESSION_USER_KEY];
    
    //写入token
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:SESSION_TOKEN_KEY];
}
-(void) persistUser:(VMCUserDomain *)user{
    //写入存储
    self.currentUser = user;
    [[NSUserDefaults standardUserDefaults] rm_setCustomObject:self.currentUser forKey:SESSION_USER_KEY];
}
-(NSString *)token {
	//没有 token 就从保存过的文件中读取token
    if(!_token){
        _token = [[NSUserDefaults standardUserDefaults] objectForKey:SESSION_TOKEN_KEY];
    }
    return _token;
}
-(void) loadUser {
	//保存用户信息 在NSUserDefaults中读取保存过的用户信息
    _currentUser = [[NSUserDefaults standardUserDefaults] rm_customObjectForKey:SESSION_USER_KEY];
}
-(void) logout {
	//用户退出登录 将当前用户和token置空
    _currentUser = nil;
    _token = nil;
    //删除用户及token，保留城市
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:SESSION_USER_KEY];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:SESSION_TOKEN_KEY];
}
-(VMCUserDomain *)currentUser {
	//加载保存过的用户
    if(!_currentUser){
        [self loadUser];
    }
    return _currentUser;
}
-(NSString *)currency {
	//货币单位
	if (self.currentUser) {
		return self.currentUser.currency;
	}
	return @"￥";
}
-(BOOL)isLogin{
	//当前token不为空 且 用户不为空 返回yes
    return [self.token length] > 0 && self.currentUser;
}

- (void)saveMedicineRemind:(NSArray *)dic isEdit:(BOOL)edit
{
	if (edit)
	{
		[[NSUserDefaults standardUserDefaults] rm_setCustomObject:dic forKey:SESSION_REMIND_KEY];
	}
	else
	{
		id object = [[NSUserDefaults standardUserDefaults] rm_customObjectForKey:SESSION_REMIND_KEY];
		if (object == nil)
		{
			[[NSUserDefaults standardUserDefaults] rm_setCustomObject:dic forKey:SESSION_REMIND_KEY];
		}
		else
		{
			NSMutableArray *arr = [NSMutableArray arrayWithArray:object];
			[arr addObjectsFromArray:dic];
			[[NSUserDefaults standardUserDefaults] rm_setCustomObject:arr forKey:SESSION_REMIND_KEY];
		}
	}

}

- (id)getMedicineRemind
{
	 id object = [[NSUserDefaults standardUserDefaults] rm_customObjectForKey:SESSION_REMIND_KEY];
	return object;
}

- (void)saveAskTypeData:(NSArray *)data
{
	if (data && data.count > 0)
	{
		[[NSUserDefaults standardUserDefaults] rm_setCustomObject:data forKey:SESSION_ASKTYPE_KEY];
	}
	else
	{
		return;
	}


}

- (id)getAskTypeData
{
	id object = [[NSUserDefaults standardUserDefaults] rm_customObjectForKey:SESSION_ASKTYPE_KEY];
	return object;
}

@end
